package com.longcai.controller.goods;


import com.longcai.util.OSSUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.entity.homepage.ClyjMenu;
import com.longcai.service.goods.ClyjMenuService;

import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LWK
 * @since 2019-03-20
 */
@Controller
@RequestMapping("/clyjMenu")
public class ClyjMenuController {
	@Autowired
	ClyjMenuService bannerService;

	/**
	 *  description: 首页菜单展示
	 *  author: LWK
	 *  time： 09点58分
	 */
	@RequestMapping("/getMenuList")
	@ResponseBody
	public List<ClyjMenu> getHomepageBannerList(){
		ApiResult<List<ClyjMenu>> result = new ApiResult<List<ClyjMenu>>();
		try{
			QueryWrapper<ClyjMenu> queryWrapper = new QueryWrapper<ClyjMenu>();
	        queryWrapper.eq("del_flag", "0");
	        queryWrapper.eq("menu_type", "1");
	        queryWrapper.orderByAsc("menu_sort");
	        result.success();
	        return bannerService.selectList(queryWrapper);
		}catch(Exception e){
			e.printStackTrace();
			result.setCode("-1");
			result.fail("查询失败："+e.getMessage());
			return null;
		}
	}
	
	/**
	 * 插入数据
	 * @param dictInfo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/insertMenu")
	@ResponseBody
	public ApiResult insertDictionaries(ClyjMenu dictInfo, MultipartFile menuIconFile){
		Map map = new HashMap();
		try {
			if (menuIconFile != null) {
				//文档上传oss
					String fileName = menuIconFile.getOriginalFilename();
					String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
					InputStream inputStream = menuIconFile.getInputStream();
					Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/CLYMENU/");
					dictInfo.setMenuIcon(uploadByInputStream.get("path").toString());
			}
			dictInfo.setCreateTime(LocalDateTime.now());
			dictInfo.setUpdateTime(LocalDateTime.now());
			dictInfo.setDelFlag("0");
			dictInfo.setMenuType("1");
			dictInfo.setMenuId(UUID.randomUUID().toString());
			boolean insert = bannerService.insert(dictInfo);
			map.put("message", "新增成功");
			map.put("flag", "S");
			return ApiResult.success(map);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			map.put("message", "新增失败");
			return ApiResult.fail("字典添加失败！");
		}
	}
	/**
	 * 删除数据
	 * @param dictInfo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/deleteMenu")
	@ResponseBody
	public Map<String,Object> deleteMenu(@RequestBody JSONObject params){
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			String att = params.getString("rowData");
			att = att.replace("[", "").replace("]", "").replace(" ", "");
			String[] attCode = att.split(",");
			for (String s : attCode) {
				boolean deleteById = bannerService.deleteById(s);
			}
			result.put("message", "删除成功");
			result.put("flag", "S");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			result.put("message", "删除失败");
			return result;
		}
	}
	/**
	 * 修改数据
	 * @param dictInfo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/updateMenu")
	@ResponseBody
	public Map<String,Object> updateMenu(ClyjMenu dictInfo, MultipartFile menuIconFile){
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			if (menuIconFile != null) {
				//文档上传oss
					String fileName = menuIconFile.getOriginalFilename();
					String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
					InputStream inputStream = menuIconFile.getInputStream();
					Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/CLYMENU/");
					dictInfo.setMenuIcon(uploadByInputStream.get("path").toString());
			}
			bannerService.updateById(dictInfo);
			result.put("message", "修改成功");
			result.put("flag", "S");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			result.put("message", "修改失败");
			return result;
		}
	}
}

